package gwtappcontainer.server.apis.insight;

import gwtappcontainer.server.apis.security.GateKeeper;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apis.insight.Center;
import gwtappcontainer.shared.apis.insight.Member;
import gwtappcontainer.shared.apis.insight.ProgramTypeProp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

@Api(name = "insightold", 
	scopes = { "https://www.googleapis.com/auth/userinfo.email" }
)
public class InsightAPI {
	
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "listprogramtypes")
	public APIResponse listProgramTypes() {
		try {
			List<ProgramTypeProp> all = ProgramTypeRepository.getAll();						
			
			APIResponse resp = new APIResponse(Status.SUCCESS, all);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addprogramtype")
	public APIResponse addProgramType(@Named("program_type") String name, User user) {
		try {
			GateKeeper.ensureLoggedin(user);
						
			ProgramTypeProp prop = ProgramTypeRepository.add(name, user.getEmail());
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.POST, path = "updateprogramtype")
	public APIResponse updateProgramType(@Named("new_name") String newName,
			@Named("existing_name") String existingName, User user) {
		
		try {
			GateKeeper.ensureLoggedin(user);			
			
			ProgramTypeProp prop = ProgramTypeRepository.rename(newName, existingName, user.getEmail());												
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "listcenters")
	public APIResponse listCenters() {
		try {
			List<Center> all = CenterRepository.getAll();						
			
			APIResponse resp = new APIResponse(Status.SUCCESS, all);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addcenter")
	public APIResponse addCenter(@Named("center") String name, User user) {
		try {			
			
			GateKeeper.ensureLoggedin(user);
			Center prop = CenterRepository.add(name, user.getEmail());
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(httpMethod = HttpMethod.POST, path = "updatecenter")
	public APIResponse updateCenter(@Named("new_name") String newName,
			@Named("existing_name") String existingName, User user) {
		try {
			GateKeeper.ensureLoggedin(user);
			
			Center prop = CenterRepository.update(newName, existingName);												
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;	
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addmember")
	public APIResponse addMember(@Named("email") String email, 
			@Named("primaryCenter") String primaryCenter,
			@Nullable @Named("firstName") String firstName, 
			@Nullable @Named("middleName") String middleName, 
			@Nullable @Named("lastName") String lastName, 
			@Nullable @Named("homeAddress") String homeAddress,
			@Nullable @Named("homePhone") String homePhone, 
			@Nullable @Named("officeAddress") String officeAddress,
			@Nullable @Named("officePhone") String officePhone, 
			User user) {
		try {
			GateKeeper.ensureLoggedin(user);
			
			Member memberProp = MemberRepository.addMember(email, primaryCenter, 
					firstName, middleName, lastName, homeAddress, homePhone, 
					officeAddress, officePhone);
					
			APIResponse resp = new APIResponse(Status.SUCCESS, memberProp);
			return resp;	
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addCenterToMember")
	public APIResponse addCenterToMember(@Named("email") String email, 
			@Named("center") String center) {
		try {
			Member prop = MemberRepository.addCenterToMember(email, center);
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.PUT, path = "addUnlistedProgramToMember")
	public APIResponse addUnlistedProgramToMember(@Named("email") String email, 
			@Named("programType") String programType, 
			@Named("venue") String venue, @Named("month") int month,
			@Named("year") int year, @Named("teacher") String teacher) {
		
		try {
			Member prop = MemberRepository.addUnlistedProgramToMember(email, 
					programType, venue, month, year, teacher);
			
			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getMember")
	public APIResponse getMember(@Named("email") String email) {
		try {
			Member prop = MemberRepository.get(email);

			APIResponse resp = new APIResponse(Status.SUCCESS, prop);
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getMembersForCenter")
	public APIResponse getMembersForCenter(@Named("center") String center) {
		try {
			
			ArrayList<Member> members = MemberRepository.getByCenter(center);
			APIResponse resp = new APIResponse(Status.SUCCESS, members);
			
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getMembersForProgramType")
	public APIResponse getMembersForProgramType(@Named("programType") String programType) {
		try {
			
			ArrayList<Member> members = MemberRepository
					.getByProgramType(programType);
			APIResponse resp = new APIResponse(Status.SUCCESS, members);
			
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getMembersForCenterAndProgramType")
	public APIResponse getMembersForCenterAndProgramType(
			@Named("programType") String programType, 
			@Named("center") String center) {
		
		try {
			
			ArrayList<Member> members = MemberRepository
					.getByCenterAndProgramType(programType, center);

			APIResponse resp = new APIResponse(Status.SUCCESS, members);
			
			return resp;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}	
	
	
}
